﻿@namespace Masa.Blazor.Components.DatePicker
@inherits MasaComponentBase

<div class="@GetClass()" style="@GetStyle()">
    @GenPickerButton(EventCallback.Factory.Create<MouseEventArgs>(this, HandleOnYearBtnClickAsync), GenYearText(), SelectingYear, false, _block.Element("year").Name)
    @GenPickerButton(EventCallback.Factory.Create<MouseEventArgs>(this, HandleOnTitleDateBtnClickAsync), GenTitleText(), !SelectingYear, false, _block.Element("date").Name)
</div>

@code {

    internal static RenderFragment GenPickerButton(EventCallback<MouseEventArgs> click, RenderFragment content, bool active, bool @readonly, string? @class = null) => builder =>
    {
        builder.OpenElement(0, "div");
        builder.AddAttribute(1, "class", _btnBlock.CreateModifierBuilder().Add(active).Add("readonly", @readonly).AddClass(@class).Build());
        builder.AddAttribute(2, "onclick", click);
        builder.AddAttribute(3, "__internal_stopPropagation_ondragover", true);
        builder.AddContent(4, content);
        builder.CloseElement();
    };

    private RenderFragment GenYearText() => __builder =>
    {
        @Year

        if (string.IsNullOrWhiteSpace(YearIcon))
        {
            return;
        }

        <MIcon Dark>@YearIcon</MIcon>
    };

    private RenderFragment GenTitleText() => __builder =>
    {
        <Transition Name="@ComputedTransition">
            <KeyTransitionElement Value="@Value">
                @((MarkupString)(Date ?? "&nbsp;"))
            </KeyTransitionElement>
        </Transition>
    };

}